////////////////////////////////////////////////////////////////////////////////
//
//  Copyright 2010 galaxy.org
//
//  Licensed under the Apache License, Version 2.0 (the "License");
//  you may not use this file except in compliance with the License.
//  You may obtain a copy of the License at
//
//  http://www.apache.org/licenses/LICENSE-2.0
//
//  Unless required by applicable law or agreed to in writing, software
//  distributed under the License is distributed on an "AS IS" BASIS,
//  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
//  See the License for the specific language governing permissions and
//  limitations under the License.
//
////////////////////////////////////////////////////////////////////////////////

package org.galaxy.richflex.utils
{
	import flash.net.SharedObject;
	
	import mx.core.FlexGlobals;
	import mx.utils.ObjectUtil;

	/**
	 * Flex cookie uses <codeSharedObject</code> to save values. The size of one cookie
	 * is limited by flash player, so the default value is 100K. This might not be enough
	 * if modules are used in a large application. So this <code>FlexCookie</code> can
	 * create different <code>SharedObject</code>s for different modules. See 
	 * <code>FlexCookie.getFlexCookie()</code> and <code>FlexCookie.getApplicationCookie()</code>
	 * for more details.
	 * 
	 * @author Cheng Liang
	 * 
	 * @see flash.net.SharedObject
	 * 
	 * @version 0.0.1 create 2010-9-30
	 */
	public class FlexCookie
	{
		
		private var cookie : SharedObject;
		
		/**
		 * Creates a cookie object. Use <code>FlexCookie.getFlexCookie()</code>
		 * or <code>FlexCookie.getApplicationCookie()</code> for more convenient.
		 * 
		 * @param cookie cookie object used for save values
		 */
		public function FlexCookie(cookie : SharedObject)
		{
			this.cookie = cookie;
		}
		
		/**
		 * Creates flex cookie with the same name of <code>obj</code>.
		 * 
		 * @param obj object to create cookie
		 * 
		 * @return cookie with the same name of <code>obj</code>
		 */
		public static function getFlexCookie(obj : Object) : FlexCookie
		{
			var className : String = ObjectUtil.getClassInfo(obj).name;
			var c : SharedObject = SharedObject.getLocal(className.substr(className.indexOf("::") + 2));
			return new FlexCookie(c);
		}
		
		/**
		 * Creates flex cookie using the only application's name.
		 * 
		 * @return cookie with the name of the only application
		 */
		public static function getApplicationCookie() : FlexCookie
		{
			return getFlexCookie(FlexGlobals.topLevelApplication);
		}
		
		/**
		 * Saves <code>value</code> with the key <code>key</code> into cookie.
		 * <strong>Note that this function will not write to local immediately.</strong>
		 * The write operation will be taken until application exits or call
		 * <code>cookie.save()</code>.
		 * 
		 * @param key key to value
		 * @param value value to save
		 * 
		 * @see #putAndSave()
		 * @see #save()
		 */
		public function put(key : String, value : Object) : void
		{
			cookie.data[key] = value;
		}
		
		/**
		 * Saves <code>value</code> with the key <code>key</code> into cookie.
		 * This will write data to local immediately.
		 * 
		 * @param key key to value
		 * @param value value to save
		 * 
		 * @see #put()
		 */
		public function putAndSave(key : String, value : Object) : void
		{
			cookie.data[key] = value;
			cookie.flush();
		}
		
		/**
		 * Gets the value to this key.
		 * 
		 * @param key key to find
		 * 
		 * @return value to this key, null if no such a key
		 */
		public function getValue(key : String) : Object
		{
			return cookie.data[key];
		}
		
		/**
		 * Writes data to local.
		 */
		public function save() : void
		{
			cookie.flush();
		}
		
		/**
		 * Clears all data in this cookie.
		 */
		public function clear() : void
		{
			cookie.clear();
		}
		
	}
}